package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.carPark.SelectCarParkModel;
import com.dcy.znoa.dao.publiz.CarParkDao;
import com.dcy.znoa.dao.publiz.PlaceDao;
import com.dcy.znoa.domain.publiz.CarPark;
import com.dcy.znoa.domain.publiz.Place;
import com.dcy.znoa.service.publiz.CarParkService;
import com.dcy.znoa.service.publiz.PlaceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarParkServiceImpl implements CarParkService {

    @Autowired
    CarParkDao carParkDao;
    @Autowired
    PlaceService placeService;
    @Autowired
    PlaceDao placeDao;

    @Override
    public Integer insert(CarPark var1) throws BusinessException {
        int result1 = carParkDao.insert(var1);
        if (result1 <= 0) {
            return result1;
        }
        //增加对应的场所
        int placeLevel = 0;//场所级别：从1开始，1为最大(园区级)，2级是1级的下级部门（公司级）
        int entityType = 1;////所属类型 1 园区 2 企业 如果不传，就默认1
        int placetype = 4;// 场所归属类型：1 园区 2 公司  3 食堂 4 停车场 99 其他
        Long pPlaceId = 1L;//默认是园区
        if (var1.getEntityType() != null) {//
            entityType = var1.getEntityType();
        }
        if (entityType == 1) {
            placeLevel = 2;
        } else if (entityType == 2) {
            placeLevel = 3;
            //查询对应的上级：即公司的placeId
            Place queryPlace = new Place();
            queryPlace.setEntityId(var1.getEntityId());
            queryPlace.setPPlaceId(1L);
            queryPlace.setPlaceType(2);
            Place compPlace = placeDao.selectOne(queryPlace);
            pPlaceId = compPlace.getPlaceId();
        } else {
            throw new BusinessException("404", "entityType 只能为1或2");
        }
        Place place = new Place(var1.getCpName(), placeLevel, placetype, pPlaceId, var1.getCpId(), var1.getCreateOperId());
        int result2 = placeService.insert(place);
        if (result2 <= 0) {
            return result2;
        }
        return 1;
    }

    @Override
    public Integer updateById(CarPark var1) {
        return carParkDao.updateById(var1);
    }

    @Override
    public Integer deleteById(Long id) throws BusinessException {
        return carParkDao.deleteById(id);
    }

    @Override
    public JsonForList selectList(SelectCarParkModel model) {
        Page page = new Page(model.getPageNum(), model.getPageSize());
        EntityWrapper ew = new EntityWrapper();
        CarPark query = new CarPark();
        ew.setEntity(query);
        if (model.getEntityType() != null) {
            ew.eq("entity_type", model.getEntityType());
            if (model.getEntityId() != null) {
                ew.eq("entity_id", model.getEntityId());
            }
        }
        if (StringUtils.isNotBlank(model.getCpName())) {
            ew.like("cp_name", model.getCpName());
        }
        List<CarPark> list = carParkDao.selectPage(page, ew);
//        List<CarPark> list = carParkDao.selectMapsPage(page,ew);
        //居然没法取出来 提示 HashMap 无法转换为CarPark
        for (CarPark carPark : list) {
//            carPark.setResidualCapacity(0);
            carPark.setResidualCapacity((int) (Math.random() * (carPark.getCapacity() == null ? 0 : carPark.getCapacity())));
        }
        return new JsonForList(list, page.getTotal());
    }
}
